from django.db import models
from django.contrib.auth.models import AbstractUser


# 定义资产所属部门（业务线）
class Department(models.Model):
    department_name = models.CharField(max_length=32, verbose_name='部门名称')

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '部门表'

    def __str__(self):
        return self.department_name


# 定义员工表
class UserInfo(AbstractUser):
    employee_name = models.CharField(max_length=32, verbose_name='员工名称', null=True)
    user_position = models.CharField(max_length=32, verbose_name='员工职位', null=True)

    # 定义与部门关联
    department_fk = models.ForeignKey(to='Department', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '员工表'

    def __str__(self):
        return self.employee_name or 'xadmin'


# 定义员工组（业务组），多个员工负责多项业务组
class UserGroup(models.Model):
    group_name = models.CharField(max_length=32, verbose_name='组名')

    # 创建组与用户多对多关联
    user_fk = models.ManyToManyField(to='UserInfo', through='User2Group',
                                     through_fields=('usergroup', 'userinfo'))

    # 定义部门与组的关联
    user_group = models.ForeignKey(to='Department', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '业务组'

    def __str__(self):
        return self.group_name


# 定义员工与组多对多关系表
class User2Group(models.Model):
    userinfo = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    usergroup = models.ForeignKey(to='UserGroup', on_delete=models.CASCADE)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '员工与业务组的关系表'


# 定义资产表
class Assets(models.Model):
    assets_type_choice = {
        (1, '服务器'),
        (2, '路由器'),
        (3, '交换机'),
        (4, '防火墙'),
        (5, '其他')
    }
    assets_type = models.IntegerField(choices=assets_type_choice, verbose_name='资产类型', default=1)
    # 所属机房
    mechanical_room_address = models.CharField(max_length=255, verbose_name='所属机房', null=True)
    # 所属机柜
    mechanical_cabinet = models.CharField(max_length=32, verbose_name='所属机柜', null=True)
    # 所属U位
    mechanical_position = models.CharField(max_length=32, verbose_name='机柜U位', null=True)
    # 是否上架
    is_active = models.CharField(max_length=32, verbose_name='是否上架', default='True')
    # 与部门做关联
    department_fk = models.ForeignKey(to='Department', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '资产表'
        ordering = ['id']

    def __str__(self):
        a = [0, '服务器', '路由器', '交换机', '防火墙', '其他']
        return a[self.assets_type] + '_' + str(self.id)


# 定义服务器信息表
class Server(models.Model):
    hostname = models.CharField(max_length=32, unique=True, verbose_name='主机名')
    os_platform = models.CharField(max_length=32, verbose_name='系统平台', null=True)
    os_version = models.CharField(max_length=255, verbose_name='系统版本', null=True)

    cpu_cores_number = models.CharField(max_length=32, verbose_name='cpu内核数', null=True)
    cpu_thread_number = models.CharField(max_length=32, verbose_name='cpu线程数', null=True)
    cpu_model = models.CharField(max_length=255, verbose_name='cpu型号', null=True)

    manufacturer = models.CharField(max_length=32, verbose_name='服务器厂商', null=True)
    product_name = models.CharField(max_length=32, verbose_name='服务器型号', null=True)
    serial_number = models.CharField(max_length=255, verbose_name='服务器SN', null=True)

    # 与资产关联
    assets_fk = models.OneToOneField(to='Assets', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '服务器信息表'

        ordering = ['id']

    def __str__(self):
        return str(self.hostname) + '-' + str(self.id)


# 定义网络设置信息表
class Router(models.Model):
    router_name = models.CharField(max_length=32, verbose_name='设备名称')

    # 与资产关联
    assets_fk = models.OneToOneField(to='Assets', on_delete=models.CASCADE)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '网络设备信息表'
        ordering = ['id']

    def __str__(self):
        return self.router_name


# 定义硬盘信息表
class Disk(models.Model):
    slot = models.CharField(max_length=32, verbose_name='磁盘槽位')
    pd_type = models.CharField(max_length=32, verbose_name='磁盘类型')
    capacity = models.CharField(max_length=32, verbose_name='磁盘容量')
    model = models.CharField(max_length=255, verbose_name='磁盘编号')

    # 与服务器关联
    server_fk = models.ForeignKey(to='Server', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '硬盘信息表'
        ordering = ['id']

    def __str__(self):
        return self.pd_type + '_' + self.slot + '_' + str(self.server_fk_id)


# 定义内存条信息表
class Memory(models.Model):
    slot = models.CharField(max_length=32, verbose_name='内存槽位')
    capacity = models.CharField(max_length=32, verbose_name='内存容量')
    model = models.CharField(max_length=32, verbose_name='内存类型')
    speed = models.CharField(max_length=32, verbose_name='内存速率')
    manufacturer = models.CharField(max_length=32, verbose_name='内存厂商')
    sn = models.CharField(max_length=32, verbose_name='内存SN')

    # 与服务器关联
    server_fk = models.ForeignKey(to='Server', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '内存信息表'
        ordering = ['id']

    def __str__(self):
        return str(self.model) + '_' + self.slot + '_' + str(self.server_fk_id)


# 定义网卡信息表
class Nic(models.Model):
    nic_name = models.CharField(max_length=32, verbose_name='网卡名称')
    status = models.CharField(max_length=32, verbose_name='网卡状态', default='True')
    mac = models.CharField(max_length=32, verbose_name='mac地址')
    ipv4 = models.CharField(max_length=32, verbose_name='ipv4地址')
    ipv4_mask = models.CharField(max_length=32, verbose_name='ipv4掩码')
    ipv6 = models.CharField(max_length=32, verbose_name='ipv6地址')

    # 与服务器关联
    server_fk = models.ForeignKey(to='Server', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '网卡信息表'
        ordering = ['id']

    def __str__(self):
        return str(self.nic_name) + '_' + str(self.server_fk_id)


# 资产变更记录表
class AssetsChange(models.Model):
    content = models.TextField(verbose_name='变更记录')

    # 定义资产变更者
    user_fk = models.ForeignKey(to="UserInfo", on_delete=models.CASCADE, null=True)
    create_time = models.DateTimeField(auto_now=True, verbose_name='变更时间')

    # 与服务器关联
    server_fk = models.ForeignKey(to='Server', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '资产变更信息表'
        ordering = ['id']

    def __str__(self):
        return str(self.server_fk.hostname) + '_' + str(self.create_time)


# 定义报错日志表
class ErrorLog(models.Model):
    create_time = models.DateTimeField(auto_now=True, verbose_name='出错时间')
    error_content = models.TextField(verbose_name="错误日志")

    # 与服务器关联
    server_fk = models.ForeignKey(to='Server', on_delete=models.CASCADE, null=True)

    class Meta:
        # admin中表名的显示
        verbose_name_plural = '错误日志表'
        ordering = ['id']

    def __str__(self):
        return str(self.create_time)
